package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ejbSessions.*;

/**
 * Servlet implementation class Autenticazione
 */
public class Autenticazione extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Autenticazione() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		gestisciRichiesta(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		gestisciRichiesta(request, response);
	}
	
	private void gestisciRichiesta(HttpServletRequest request, HttpServletResponse response){
		PrintWriter out;
		String username, password;
		int userid;
		String logout;
		try {
			out = response.getWriter();
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("ManagerAutenticazione/remote");
			ManagerAutenticazioneRemote autenticazione = (ManagerAutenticazioneRemote) ref;
			
			username = request.getParameter("username");
			password = request.getParameter("password");
			logout = request.getParameter("logout");
			request.getSession().setAttribute("errore", "");
			if(logout!=null){
				request.getSession().setAttribute("userid", -1);
				request.getSession().removeAttribute("userid");
				request.getSession().removeAttribute("amministratore");
				response.sendRedirect("./");
			}else{
				if(autenticazione.isAmministratore(username, password)){
					out.print("Amministratore");
					request.getSession().setAttribute("amministratore",1);
					response.sendRedirect("./Amministratore");
				}else{
					userid = autenticazione.getIdUtente(username, password);
					if(userid != -1){
						out.print("Utente");
						out.print(userid);
						request.getSession().setAttribute("userid", userid);
						response.sendRedirect("./Utente");
					}else{
						out.print("errore");
						request.getSession().setAttribute("errore", "Login errato");
						response.sendRedirect("./");
					}
				}
			}

		}catch (IOException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}
}
